/*
Make appendix figure of list charges for injury alcohol group
*/

*To create PDF files
cap pr drop grexportpdf
program define grexportpdf
   version  11
   args using
   loc using = subinstr("`using'",".pdf","",.)
   gr export "`using'.eps" , replace
   !epstopdf "`using'.eps"
   erase "`using'.eps"
end

*Load up data file
cd "C:\Research\Alcohol and Morbidity\4. Code up from scratch"

*Import the data
use "`base'\Data Files and Code That Produced Them\P10 Check Profiles of Charges in ED data by Cause", replace
 

gen charges_injury_or_alc_ED = charges_injury_or_alc
gen counts_injury_or_alc_ED = counts_injury_or_alc

keep months_21 charges_injury_or_alc_ED counts_injury_or_alc_ED
save ed_charges , replace

*Import the data
use "E08 Check Profiles of Charges Inpatient data by Cause", replace

gen charges_injury_or_alc_INP = charges_injury_or_alc
gen counts_injury_or_alc_INP = counts_injury_or_alc

keep months_21 charges_injury_or_alc_INP counts_injury_or_alc_INP
merge 1:1 months_21 using ed_charges


*Create running vars
gen age_c = months_21
gen age_c_sq = months_21*months_21
gen post_21 = 0
replace post_21 = 1 if months_21 >= 0
gen age_c_post = months_21*post_21
gen age_c_sq_post = age_c_sq*post_21

*Restrict the sample
keep if months_21 >= -24  & months_21 <=23 

gen age = 21+age_c/12

local vars  = "injury_or_alc_ED injury_or_alc_INP"

*Create all fitted lines on either side of RD

foreach var of local vars {  
     *Fit the left side polynomial;
      reg charges_`var' age_c age_c_sq  if months_21 < 0 [aweight = counts_`var'] , robust
      predict charges_`var'_left        if months_21 <= 0
     *Fit the right side polynomial;
      reg charges_`var' age_c age_c_sq  if months_21 > 0   [aweight = counts_`var'] , robust
      predict charges_`var'_right       if months_21 >= 0  
	 *Run regression
      reg charges_`var' post_21    age_c    age_c_sq  age_c_post age_c_sq_post [aweight = counts_`var'] , robust
      local b_P_charges_`var' : di %4.1f  round(_b[post],.1)
      local se_P_charges_`var' : di %4.1f  round(_se[post],.1)
}
   *Make the figure
#delimit ;
graph twoway  
 	        (scatter charges_injury_or_alc_ED age, mcolor(black)  msymbol(S)  msize(.6) yaxis(1) 
			   yscale(range(1000,2000) axis(1)) 
			   ylabel(#3, axis(1) nogrid)) 
 			(line charges_injury_or_alc_ED_left age, lwidth(thin) lcolor(black) yaxis(1)) 
            (line charges_injury_or_alc_ED_right age, lwidth(thin) lcolor(black) yaxis(1)) 
 	        (scatter charges_injury_or_alc_INP age, mcolor(blue)  msymbol(T)  msize(.6) yaxis(2) 
			   yscale(range(10000,20000) axis(2)  ) 
			   ylabel(#3, axis(2) nogrid labcolor(blue))) 
			(line charges_injury_or_alc_INP_left age, lwidth(thin) lcolor(blue) yaxis(2)) 
            (line charges_injury_or_alc_INP_right age, lwidth(thin) lcolor(blue) yaxis(2)) 
             , 
			 title("Appendix 9: Age Profile of List Charges")
			 subtitle("(Visit or Admission due to Injury or Alcohol)")
			 xtitle("Age at Time of ED Visit or Inpatient Admission")   
			 ytitle("Emergency Department List Charges") 
			 ytitle("Inpatient List Charges", color(blue) axis(2)) 
			 legend(off) 
              text(1400  20  
	                             "{stMono: Increase at 21 ED: `b_P_charges_injury_or_alc_ED' }" 
	                             "{stMono: Standard Error:   (`se_P_charges_injury_or_alc_ED')}" 
				  	  ,color(black) size(small) j(left) nobox fcolor(gs16) lcolor(gs12) margin(vsmall) )  
              text(1700  20  
	                             "{stMono: Increase at 21 Inpatient: `b_P_charges_injury_or_alc_INP' }" 
	                             "{stMono: Standard Error:          (`se_P_charges_injury_or_alc_INP')}" 
				  	  ,color(blue) size(small) j(left) nobox fcolor(gs16) lcolor(gs12) margin(vsmall) )  
			 note("Note: The blue triangles are inpatient list charges the black squares are ED list charges.",size(vsmall)) graphregion(style(none) color(gs16))  
;
#delimit cr
grexportpdf "`base'\Code for Appendices\Appendix 9.pdf"

